package com.tuzhi.common.domain;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Jwt标准载荷：有很多，建议使用，但不强制，对JWT信息作补充。
 * Payload组成
 * Payload 部分也是一个 JSON 对象，用来存放实际需要传递的有效信息。
 */
public class JwtPayload {

    /**
     * jti (JWT ID)	编号，JWT 的唯一身份标识
     */
    private String jti;

    /**
     * iss (issuer)	签发人（谁签发的）
     */
    private String issuer;
    /**
     * sub (subject)	主题（用来做什么）
     */
    private String sub;
    /**
     * aud (audience)	受众(给谁用的)比如：http://www.xxx.com
     */
    private String aud;
    /**
     * nbf (Not Before)	生效时间
     */
    private Date nbf;
    /**
     * iat (Issued At)	签发时间
     */
    private Date iat;

    /**
     * exp (expiration time)	过期时间，必须要大于签发时间
     */
    private Date exp;

    public JwtPayload() {
    }

    public JwtPayload(String jti, String issuer, Date nbf, Date iat, Date exp) {
        this.jti = jti;
        this.issuer = issuer;
        this.nbf = nbf;
        this.iat = iat;
        this.exp = exp;
    }

    /**
     * @param jti
     * @param issuer
     * @param ttlTime  有效时长
     * @param timeUnit 时间单位：TimeUnit.SECONDS 、TimeUnit.MINUTES
     */
    public JwtPayload(String jti, String issuer, int ttlTime, TimeUnit timeUnit) {
        this.jti = jti;
        this.issuer = issuer;
        Date now = new Date();
        this.nbf = now;
        this.iat = now;
        if (null == timeUnit || TimeUnit.MICROSECONDS == timeUnit || TimeUnit.NANOSECONDS == timeUnit || TimeUnit.NANOSECONDS == timeUnit) {
            throw new RuntimeException("构造JwtPayload，异常的时间单位。" + timeUnit);
        }
        if (TimeUnit.SECONDS == timeUnit) {
            ttlTime = ttlTime * 1000;//秒转成毫秒
        }
        if (TimeUnit.MINUTES == timeUnit) {
            ttlTime = ttlTime * 1000 * 60;//分钟转成毫秒
        }
        if (TimeUnit.HOURS == timeUnit) {
            ttlTime = ttlTime * 1000 * 60 * 60;//小时转成毫秒
        }
        if (TimeUnit.DAYS == timeUnit) {
            ttlTime = ttlTime * 1000 * 60 * 60 * 24;//天转成毫秒
        }
        this.exp = new Date(now.getTime() + ttlTime);
    }

    public static JwtPayload create() {
        JwtPayload jwtPayload = new JwtPayload();
        return jwtPayload;
    }

    public JwtPayload jti(String jti) {
        this.jti = jti;
        return this;
    }

    public JwtPayload issuer(String issuer) {
        this.issuer = issuer;
        return this;
    }

    public JwtPayload sub(String sub) {
        this.sub = sub;
        return this;
    }

    public JwtPayload aud(String aud) {
        this.aud = aud;
        return this;
    }

    public JwtPayload nbf(Date nbf) {
        this.nbf = nbf;
        return this;
    }

    public JwtPayload iat(Date iat) {
        this.iat = iat;
        return this;
    }

    public JwtPayload exp(Date exp) {
        this.exp = exp;
        return this;
    }

    public String getJti() {
        return jti;
    }

    public void setJti(String jti) {
        this.jti = jti;
    }

    public String getIssuer() {
        return issuer;
    }

    public void setIssuer(String issuer) {
        this.issuer = issuer;
    }

    public String getSub() {
        return sub;
    }

    public void setSub(String sub) {
        this.sub = sub;
    }

    public String getAud() {
        return aud;
    }

    public void setAud(String aud) {
        this.aud = aud;
    }

    public Date getNbf() {
        return nbf;
    }

    public void setNbf(Date nbf) {
        this.nbf = nbf;
    }

    public Date getIat() {
        return iat;
    }

    public void setIat(Date iat) {
        this.iat = iat;
    }

    public Date getExp() {
        return exp;
    }

    public void setExp(Date exp) {
        this.exp = exp;
    }
}
